# -*- coding: UTF-8 -*-

import os
from selenium import webdriver
import datetime
import pymysql
from bs4 import BeautifulSoup
import time

chromedriver = "C:\\WorkEnvir\\chromedriver\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)


def getData():
    html = driver.get("http://flights.ctrip.com/booking/CTU-CAN-day-1.html?DDate1=2018-02-06")
    driver.set_page_load_timeout(10)
    driver.maximize_window()
    js = "var q=document.body.scrollTop=100000"
    driver.execute_script(js)
    time.sleep(3)
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    div_ = soup.find('div', id="J_flightlist2")
    # print(div_)
    head_rows = soup.find_all('tr', class_="J_header_row")
    for head_row in head_rows:
        print(head_row)
        air_company = soup.find('strong', class_="flight_logo").contents
        flight_number = soup.find('div', class_="J_flight_no").data-flight
        print(air_company)
        print(flight_number)
    print(head_rows)

    now = datetime.datetime.now()
    print('get success:', now.strftime('%Y-%m-%d %H:%M:%S'))


# def saveData(air_company, flight_number, departure_time, arrive_time, departure_airport, arrive_airport, source,
#              cabin_info):
#     # 打开数据库连接
#     db = pymysql.connect(host="10.0.101.23", user="root", password="yk@1234", db="airspider", port=3306)
#     # 使用cursor()方法获取操作游标
#     cur = db.cursor()
#     # 1.查询操作
#     # 编写sql 查询语句  user 对应我的表名
#     sql = "SELECT * FROM feizhu"
#     try:
#         cur.execute(sql)  # 执行sql语句
#
#     sql_insert = """INSERT INTO raw_airdata(capture_time,air_company,flight_number,departure_time,arrive_time,departure_airport,arrive_airport,source,cabin_info)
#         VALUES(datetime.datetime.now(),air_company, flight_number, departure_time, arrive_time, departure_airport, arrive_airport, source,
#              cabin_info)"""
#
#     try:
#         cur.execute(sql_insert)
#         # 提交
#         db.commit()
#     except Exception as e:
#         # 错误回滚
#         db.rollback()
#     finally:
#         db.close()

getData()
